package com.example.yin.controller;

import com.example.yin.common.ErrorMessage;
import com.example.yin.common.SuccessMessage;
import com.example.yin.common.aop.MyLog;
import com.example.yin.config.OSSConfiguration;
import com.example.yin.constant.Constants;
import com.example.yin.domain.Show;
import com.example.yin.service.OssService;
import com.example.yin.service.ShowService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@RestController
@RequestMapping("/show")
public class ShowController {
    @Autowired
    ShowService showService;

    @Autowired
    private OSSConfiguration ossConfiguration;

    @Autowired
    private OssService ossService;

    private ConcurrentHashMap<Integer, List<Integer>> likeMap = new ConcurrentHashMap<>();

    @Configuration
    public static class MyPicConfig implements WebMvcConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/img/showPic/**")
                    .addResourceLocations(Constants.SHOW_PIC_PATH);
            registry.addResourceHandler("/show/**")
                    .addResourceLocations(Constants.SHOW_PIC_PATH);
        }
    }


    @RequestMapping(value = "/like", method = RequestMethod.POST)
    public Object like(HttpServletRequest req){
        String show_id = req.getParameter("show_id");
        String show_user = req.getParameter("show_user");
        List<Integer> consumerList = likeMap.getOrDefault(show_id, new ArrayList<>());

        Show show = showService.getDetail(Integer.valueOf(show_id));
        if (consumerList.contains(show_user)){
            show.setShowLikeCount(show.getShowLikeCount()-1);
            boolean res = showService.update(show);
            if (res) {
                consumerList.remove(show_user);
                return new SuccessMessage<ObjectUtils.Null>("取消点赞成功").getMessage();
            }
        }else {
            show.setShowLikeCount(show.getShowLikeCount()+1);
            boolean res = showService.update(show);
            if (res) {
                consumerList.add(Integer.valueOf(show_user));
                return new SuccessMessage<ObjectUtils.Null>("点赞成功").getMessage();
            }
        }
        return new ErrorMessage("点赞失败").getMessage();
    }

    @MyLog(value = "申请演出")
    @RequestMapping("/add")
    public Object add(HttpServletRequest req, @RequestParam("file") MultipartFile mpfile) throws ParseException {
        //从请求中获取
        String show_user = req.getParameter("show_user").trim();
        String show_data_str = req.getParameter("show_date").trim();
        String end_data_str = req.getParameter("end_date").trim();
        String show_title = req.getParameter("show_title").trim();
        String show_like_count = req.getParameter("show_like_count").trim();
        String actor = req.getParameter("actor").trim();
        String show_location = req.getParameter("show_location").trim();
        //String pic = "/img/songPic/tubiao.jpg";
        String show_description = req.getParameter("show_description").trim();
        String fileName = mpfile.getOriginalFilename();
        String storeUrlPath = ossConfiguration.getShowPicFilePath()+fileName;
        //上传oss
        ossService.uploadFile(mpfile,ossConfiguration.getShowPicFilePath()+fileName);
        //存数据库
        Show show = new Show();

        show.setShowUser(Integer.valueOf(show_user));
        show.setShowTitle(show_title);
        show.setShowLikeCount(Integer.valueOf(show_like_count));

        show.setActor(actor);
        show.setShowDescription(show_description);
        show.setShowLocation(show_location);
        show.setShowStatus(0);
        show.setShowPicture(storeUrlPath);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date showData = sdf.parse(show_data_str);
        Date endData = sdf.parse(end_data_str);
        show.setShowData(showData);
        show.setEndData(endData);
        boolean res = showService.add(show);
        if (res) {
            return new SuccessMessage<ObjectUtils.Null>("添加成功").getMessage();
        } else {
            return new ErrorMessage("添加失败").getMessage();
        }
    }

    @RequestMapping("/delete")
    public Object delete(Integer show_id){
        boolean res = showService.delete(show_id);
        if (res) {
            return new SuccessMessage<ObjectUtils.Null>("删除成功").getMessage();
        } else {
            return new ErrorMessage("删除失败").getMessage();
        }
    }

    @RequestMapping("/update")
    public Object update(@RequestBody Show show){
        boolean res = showService.update(show);
        if (res) {
            return new SuccessMessage<ObjectUtils.Null>("更新成功").getMessage();
        } else {
            return new ErrorMessage("更新失败").getMessage();
        }
    }

    @RequestMapping("/list")
    public Object getList(@RequestBody Show show){
        List<Show> res = showService.getList(show);
        if (res != null) {
            return new SuccessMessage("查询成功",res).getMessage();
        } else {
            return new ErrorMessage("查询失败").getMessage();
        }
    }


    @MyLog(value = "管理员审核通过")
    @GetMapping("/agree")
    public Object agree(@RequestParam Integer show_id){
        Show show=showService.getDetail(show_id);
        show.setShowStatus(1);
        boolean res = showService.update(show);
        if (res) {
            return new SuccessMessage<ObjectUtils.Null>("审核通过").getMessage();
        }else{
            return new ErrorMessage("审核没成功").getMessage();
        }
    }

    @MyLog(value = "管理员审核不通过")
    @GetMapping("/disagree")
    public Object disagree(@RequestParam Integer show_id){
        Show show=showService.getDetail(show_id);
        show.setShowStatus(1);
        boolean res = showService.update(show);
        if (res) {
            return new SuccessMessage<ObjectUtils.Null>("审核不通过").getMessage();
        }else{
            return new ErrorMessage("审核没成功").getMessage();
        }
    }

}
